<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Product;
use App\Services\IndexService;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Form\NestedForm;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ProductController extends Controller
{
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function index(Content $content)
	{
		return $content->header('模特管理')->description('列表')->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed   $id
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function show($id, Content $content)
	{
		return $content->header('查看模特')->description('详情')->body($this->detail($id));
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed   $id
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function edit($id, Content $content)
	{
		return $content->header('编辑模特')->description('编辑')->body($this->form()->edit($id));
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function create(Content $content)
	{
		return $content->header('创建模特')->description('创建')->body($this->form());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid()
	{
		$grid = new Grid(new Product);

		$grid->id('Id');
		$grid->column('category.name','分类名称');
		$grid->title('标题')->help('昵称');
		$grid->image('封面图')->lightbox(['width' => 80, 'height' => 80]);
		$grid->url('首页展示轮播图片')->lightbox(['width' => 80, 'height' => 80]);

		$grid->is_release('是否发布')->radio([
			'0' => '不发布',
			'1' => '发布'
		]);
		$grid->is_suggest('首页推荐')->radio([
			'0' => '不推荐',
			'1' => '推荐'
		]);
        $grid->is_home('首页轮播展示')->radio([
            '0' => '不展示',
            '1' => '展示'
        ]);
        $grid->column('sort', '排序')->sortable()->editable();
        $grid->seo_title('SEO标题');
        $grid->seo_keyword('SEO关键字');
        $grid->pv('浏览量');
        $grid->comment('备注')->editable();
        $grid->created_at('创建时间');
		$grid->updated_at('编辑时间');

		$grid->model()->orderBy('is_release', 'desc');
		$grid->disableExport();
		$grid->disableRowSelector();

        $grid->actions(function ($actions) {
            // 去掉查看
//            $actions->disableView();
        });

		$grid->filter(function(Filter $filter) {
			// 去掉默认的id过滤器
            $filter->equal('is_release', '是否发布')->select([
                '0' => '不发布',
                '1' => '发布'
            ]);
            $filter->equal('is_suggest', '首页推荐')->select([
                '0' => '不推荐',
                '1' => '推荐'
            ]);
            $filter->equal('is_home', '首页轮播展示')->select([
                '0' => '不展示',
                '1' => '展示'
            ]);

			// 在这里添加字段过滤器
			$filter->like('title', '标题');
		});

		$grid->model()->orderByDesc('sort')->orderByDesc('id');
		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 *
	 * @return Show
	 */
	protected function detail($id)
	{
		$show = new Show(Product::findOrFail($id));

		$show->id('Id');
		$show->title('模特名称');
		$show->image('模特封面')->image();
		$show->seo_title('SEO标题');
		$show->seo_description('SEO描述');
		$show->seo_keyword('SEO关键字');
		$show->pv('浏览量');
		$show->comment('备注');
		$show->created_at('创建时间');
		$show->updated_at('编辑时间');

		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form()
	{
		$form = new Form(new Product);

		$form->select('category_id', '分类')->options(function() {
            $category = Category::shelf()->pluck('name', 'id');

            return $category ?? [];
        })->required();
		$form->text('title', '模特名称')->required()->help('模特昵称');
		$form->image('image', '模特封面图片')->uniqueName()->help('建议1:1比例图片(图片大小在线压缩网站：https://imagecompressor.com/zh/)')->rules('required|mimes:jpeg,bmp,png,jpg,gif');
		$form->image('url', '首页展示轮播图片')->uniqueName()->rules('required|mimes:jpeg,bmp,png,jpg,gif')->help('建议尺寸:1920*1080(图片大小在线压缩网站：https://imagecompressor.com/zh/)');


        $form->hasMany('product_images', '模特详情照片', function(NestedForm $form) {
            $form->image('detail_image', '模特详情照片')->uniqueName()->help('建议1:1比例图片(图片大小在线压缩网站：https://imagecompressor.com/zh/)');
        });

        $form->hasMany('product_photos', '模特其他照片', function(NestedForm $form) {
            $form->image('photo', '模特其他照片')->uniqueName()->help('建议1:1比例图片(图片大小在线压缩网站：https://imagecompressor.com/zh/)');
        });

		$form->radio('is_release', '是否发布')->options([
			'0' => '不发布',
			'1' => '发布'
		])->default('0');
        $form->radio('is_suggest', '首页推荐')->options([
            '0' => '不推荐',
            '1' => '推荐'
        ])->default('0');
        $form->radio('is_home', '是否首页轮播展示')->options([
            '0' => '不展示',
            '1' => '展示'
        ])->default('0');

        $form->text('product_auxiliaries.height', '身高')->required();
        $form->text('product_auxiliaries.bust', '胸围')->required();
        $form->text('product_auxiliaries.waist', '腰围')->required();
        $form->text('product_auxiliaries.hips', '臀围')->required();
        $form->text('product_auxiliaries.dress', '衣服尺码')->required();
        $form->text('product_auxiliaries.shoe', '鞋子尺码')->required();
        $form->text('product_auxiliaries.hair_colour', '头发颜色')->required();
        $form->text('product_auxiliaries.eyes', '眼睛颜色')->required();

        $form->divider();
		$form->text('seo_title','SEO标题');
		$form->text('seo_description','SEO描述');
		$form->text('seo_keyword','SEO关键字');
		$form->text('comment', '备注')->default('');

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
        });

        $form->tools(function (Form\Tools $tools) {
            // 去掉`查看`按钮
            $tools->disableView();
        });

        $form->saved(function (Form $form){
            IndexService::indexCache();
        });

		return $form;
	}
}
